﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using BUS;
namespace final.wuc
{
    public partial class wucdangnhap : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["isLogin"] == null || (bool)Session["isLogin"] == false)
                    SetLogIn(false);
                else
                    SetLogIn(true);
            }
        }
        private void SetLogIn(bool p)
        {
            pnlLogIn.Visible = !p;
            pnlLogOut.Visible = p;
            if (p)
            {
                lblerrorlogin.Text = "";
                lblusername.Text = ((TUSER)Session["User"]).Username;
            }
            else
            {
                lblerrorlogin.Text = "";
                lblusername.Text = "";
            }

        }
        protected void imglogin_Click(object sender, ImageClickEventArgs e)
        {
            UserBUS userBUS = new UserBUS();
            TUSER user = userBUS.checkLogin(txtusername.Text, txtpassword.Text);
            if (user == null)
            {
                lblerrorlogin.Text = "Invalid username or password";
            }
            else
            {
                Session["isLogin"] = true;
                Session["user"] = user;
                Response.Redirect("~/test.aspx");
            }
        }

        protected void imglogout_Click(object sender, ImageClickEventArgs e)
        {
            Session["isLogin"] = false;
            Session["user"] = null;
            Response.Redirect("~/test.aspx");
        }
    }
}